package com.rhaggarty.blackjack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

import com.rhaggarty.blackjack.cardroom.Dealer;
import com.rhaggarty.blackjack.cardroom.Seat;
import com.rhaggarty.blackjack.cardroom.SinglePlayerDealer;
import com.rhaggarty.blackjack.game.Player;

/**
 
 * @author rhaggarty
 */
public final class BlackjackRunner {
    
    private static final Dealer dealer = new SinglePlayerDealer();
    
    private static Seat seat;
    
    public static void main(final String[] args) throws IOException {
        init();
        
        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try {
            while ((input = reader.readLine()) != null) {
                try {
                    onEvent(input.toLowerCase());
                } catch (final IllegalStateException ex) {
                    System.out.println("Invalid action.  Please try again");
                }
            }
        } finally {
            reader.close();
        }
        
        System.out.println("Finished");
    }
    
    private static void init() {
        final Player player = new Player(new BigDecimal("1000.00"));
        seat = dealer.sitPlayer(player);
    }
    
    private static void onEvent(final String event) {
        if (event.equals("exit")) {
            System.exit(0);
        } else if (event.startsWith("bet ")) {
            System.out.println(dealer.bet(new BigDecimal(event.substring(4))));
        } else if (event.equals("stand")) {
            System.out.println(dealer.stand());
        } else if (event.equals("hit")) {
            System.out.println(dealer.hit());
        } else if (event.equals("double down")) {
            System.out.println(dealer.doubleDown());
        }
        printHands();
    }
    
    private static void printHands() {
        System.out.println("Player has " + seat.getPlayer().getStack());
        System.out.println("Player - " + seat.getHand() + ", " + seat.getHand().getFormattedValues());
        System.out.println("Dealer - " + dealer.getDealerHand() + ", " + dealer.getDealerHand().getFormattedValues());
    }
}
